package com.mango.community.service;

import com.mango.community.dao.MessageMapper;
import com.mango.community.entity.Message;
import com.mango.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2020/8/5 22:04
 */
@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    /**查询当前用户的会话列表，针对每个会话只返回当前最新的一条私信
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    public List<Message> findConversations(int userId,int offset ,int limit){
        return messageMapper.selectConversations(userId, offset, limit);
    }

    /**查询当前用户的会话数量
     * @param userId
     * @return
     */
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }

    /**查询某个会话所包含的私信列表
     * @param conversation
     * @param offset
     * @param limit
     * @return
     */
    public List<Message> findLetters(String conversation,int offset ,int limit){
        return messageMapper.selectLetters(conversation, offset, limit);
    }

    /**查询某个会话所包含的私信数量
     * @param conversation
     * @return
     */
    public int findLetterCount(String conversation){
        return messageMapper.selectLetterCount(conversation);
    }

    /**查询未读私信的数量
     * @param userId
     * @param conversation
     * @return
     */
    public int findLetterUnreadCount(int userId,String conversation){
         return messageMapper.selectLetterUnreadCount(userId, conversation);
    }

    /** 新增消息 并且对消息进行敏感词的过滤
     * @param message
     * @return
     */
    public int addMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    /** 读消息方法，将消息的状态改为已读(1)
     * @param ids
     * @return
     */
    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids,1);
    }

    /** 查询某个主题下最新的通知
     * @param userId
     * @param topic
     * @return
     */
    public Message findLatestNotice(int userId ,String topic){
        return messageMapper.selectLatestNotice(userId, topic);
    }

    /** 查询某个主题所包含的通知数量
     * @param userId
     * @param topic
     * @return
     */
    public int findNoticeCount(int userId ,String topic){
        return messageMapper.selectNoticeCount(userId,topic);
    }

    /** 查询未读的通知的数量
     * @param userId
     * @param topic
     * @return
     */
    public int findNoticeUnreadCount(int userId ,String topic){
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    /** 查询某个主题所包含的通知列表
     * @param userId
     * @param topic
     * @param offset
     * @param limit
     * @return
     */
    public List<Message> findNotices(int userId,String topic,int offset,int limit){
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }
}
